/*
    This file is part of libcloak.

    libcloak is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libcloak is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libcloak.  If not, see <http://www.gnu.org/licenses/>.

*/



#ifdef HAVE_IMAGE
#include "../include/ImageResource.h"
#include <iostream>
#include <SDL_image.h>
namespace Cloak
{
	using namespace std;
	ImageResource::ImageResource(const string &name) : image(NULL)
	{
		this->name = name;
		
		char *buffer;
		size_t rlen;
		
		buffer = loadResource(this->name, &rlen);
		
		
		if(buffer == NULL)
			loaded = false;
		
		
		image = IMG_Load_RW(SDL_RWFromMem(buffer,rlen),1);
		delete buffer;
		
		
		if(image == NULL)
			loaded = false;
		
		loaded = true;
		
	}
	
	ImageResource::ImageResource(ImageResource &old) : Resource(old)
	{
		SDL_Surface *oimg = old.image;
		
		if(oimg == NULL)
			return;
		
		SDL_Rect rect;
		rect.x = rect.y = 0;
		rect.w = oimg->w;
		rect.h = oimg->h;
		
		image = SDL_CreateRGBSurface(oimg->flags, oimg->w, oimg->h, oimg->pitch,
			0,0,0,0);
		SDL_BlitSurface(oimg, &rect, image, &rect);
		
	}
	
	
	ImageResource::~ImageResource() 
	{
		clean();
	}
	
	
	void ImageResource::clean()
	{
		if(image != NULL)
		{
			SDL_FreeSurface(image);
			image = NULL;
		}
	}
	
	
	
	const SDL_Surface* ImageResource::getImage()
	{
		return image;
	}
	
}
#endif

